package com.dts.project.dao;


import java.sql.*; 
import com.dts.core.dao.AbstractDataAccessObject;
import com.dts.core.util.CoreList;
import com.dts.core.util.DateWrapper;
import com.dts.core.util.LoggerManager;
import com.yourcompany.struts.form.MobileCardSalesForm;

public class MobileCardSalesDao extends AbstractDataAccessObject 
{
	public boolean insertMobileCardSales(MobileCardSalesForm mcform)
	{
		java.sql.Connection con=null;
		boolean flag=false;
		try
		{
		int n=getSequenceID("MobileCardSales","SaleId");
		con=getConnection();
		PreparedStatement ps=con.prepareStatement("insert into MobileCardSales values(?,?,?,?,?)");
		ps.setInt(1,n);
		ps.setString(2,mcform.getSaleDate());
		ps.setInt(3,mcform.getCardId());
		ps.setInt(4,mcform.getOperatorId());
		ps.setInt(5,mcform.getUserId());
		 
		int i=ps.executeUpdate();
		if(i>0)
			flag=true;
		 
			
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}finally
	{
		try{
			con.close();
		}
	catch(Exception e)
	{
		LoggerManager.writeLogInfo(e);
	}
	}
	return flag;
	
	
		}

public CoreList ViewMobileCardSales()
{   CoreList v=new CoreList();
	java.sql.Connection con=null;
	MobileCardSalesForm mcform=null;
	try
	{
		con=getConnection();
		Statement st=con.createStatement();
		ResultSet rs=st.executeQuery("select * from MobileCardSales");
		 
		while(rs.next())
		{
			mcform=new MobileCardSalesForm();
			mcform.setSaleId(rs.getInt(1));
			mcform.setSaleDate(DateWrapper.parseDate(rs.getDate(2)));
			mcform.setCardId(rs.getInt(3));
			mcform.setOperatorId(rs.getInt(3));
			mcform.setUserId(rs.getInt(3));
			 
            v.add(mcform);
		}

		 
	}
		catch(Exception e)
	{
		
	}
		finally
		{
			try{
				con.close();
			}
		catch(Exception e)
		{
			LoggerManager.writeLogInfo(e);
		}
		}
		return v;
		
	
}
public MobileCardSalesForm ViewMobileCardSalesById(int SaleId)
{   MobileCardSalesForm mcform=new MobileCardSalesForm();
	java.sql.Connection con=null;
	 
	try
	{
		 
		con=getConnection();
		 
		PreparedStatement ps=con.prepareStatement("select * from MobileCardSales where SaleId = ? ");
		ps.setInt(1,SaleId);
		ResultSet rs=ps.executeQuery();
		 
		if(rs.next())
		{
			mcform=new MobileCardSalesForm();
			mcform.setSaleId(rs.getInt(1));
			mcform.setSaleDate(DateWrapper.parseDate(rs.getDate(2)));
			mcform.setCardId(rs.getInt(3));
			mcform.setOperatorId(rs.getInt(3));
			mcform.setUserId(rs.getInt(3));
			 
			 
		}

		 
	}
		catch(Exception e)
	{
		
	}finally
	{
		try{
			con.close();
		}
	catch(Exception e)
	{
		LoggerManager.writeLogInfo(e);
	}
	}
	return mcform;
	
	
}

public boolean deleteMobileCardSales(int SaleId)
	{
		java.sql.Connection con=null;
		boolean flag=false;
		try
		{
			 
			con=getConnection();
			 
			PreparedStatement ps=con.prepareStatement("delete from MobileCardSales where SaleId = ? ");
			ps.setInt(1,SaleId);
			int i=ps.executeUpdate();
			if(i>0)
				flag=true;
			
			          
		}
		catch(Exception e)
		{
		}
		finally
		{
			try{
				con.close();
			}
		catch(Exception e)
		{
			LoggerManager.writeLogInfo(e);
		}
		}
		return flag;
		
		}

public boolean UpdateMobileCardSales(MobileCardSalesForm mcform)
{
	java.sql.Connection con=null;
	boolean flag=false;
	try
	{
	 
	con=getConnection();
	PreparedStatement ps=con.prepareStatement("update MobileCardSales set SaleDate = ?,cardId = ?,operatorId = ?, userId = ? where SaleId = ? ");
	ps.setInt(5,mcform.getSaleId());
	ps.setString(1,DateWrapper.parseDate(mcform.getSaleDate()));
	ps.setInt(2,mcform.getCardId());
	ps.setInt(3,mcform.getOperatorId());
	ps.setInt(4,mcform.getUserId());
	int i=ps.executeUpdate();
	if(i>0)
		flag=true;
	 
}
catch(Exception e)
{
}finally
{
	try{
		con.close();
	}
catch(Exception e)
{
	LoggerManager.writeLogInfo(e);
}
}
return flag;


	}

public boolean getCardDetails(int cardno,int amount)
{
	boolean flag=false;
	Connection con=null;
	try{
		con=getConnection();
		PreparedStatement pst=con.prepareStatement("update creditcard set balance=balance-"+amount+" where cardno="+cardno+"");
		int n=pst.executeUpdate();
		if(n>0)
		{
			flag=true;
		}
	}catch (Exception e) {
		e.printStackTrace();
		// TODO: handle exception
	}
	return flag;
}
public boolean updateCardStatus(int cardno)
{
	boolean flag=false;
	Connection con=null;
	try{
		con=getConnection();
		PreparedStatement pst=con.prepareStatement("update mobilecarddetails set cardstatus='InActive' where cardid="+cardno+"");
		int n=pst.executeUpdate();
		if(n>0)
		{
			flag=true;
		}
	}catch (Exception e) {
		e.printStackTrace();
		// TODO: handle exception
	}
	return flag;
}

}






 


 
